package com.nwpu.coursesupport.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.service.imple.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(value="/password")
public class PasswordController {
    @Autowired
    private UserService userService;

    @RequestMapping(value="/checkOldPassword",method= RequestMethod.POST)
    @ResponseBody
    public Object checkOldPassword(@RequestParam("data") String data){
        JSONObject rst = new JSONObject();
        System.out.println("++++++++++++DATADATA++++++++"+data);
        JSONObject items = JSONObject.parseObject(data);
        String oldPassword = items.getString("oldPassword");
        String userCode = items.getString("userCode");
        String existPassword = items.getString("existPassword");
        String newPassword = items.getString("newPassword");
        //将旧密码加密后与数据库中密码对比
        if (existPassword.equals(userService.getEncryptionPassword(oldPassword, userCode))){
            rst.put("status", "success");
            rst.put("message", "correct");
            rst.put("newPassword",userService.getEncryptionPassword(newPassword, userCode));
        }else {
            rst.put("status", "success");
            rst.put("message", "wrong");
        }
        return rst;
    }

}
